package com.jww.service.provider;

import com.jww.dao.BaseDao;
import com.jww.dao.provider.providerDao;
import com.jww.dao.provider.providerDaoImpl;
import com.jww.pojo.Provider;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class providerServiceImpl implements providerService {
    private providerDao providerDao;

    public providerServiceImpl() {
        providerDao = new providerDaoImpl();
    }

    // 查询提供公司
    @Override
    public List<Provider> queryProvider() {
        Connection conn = null;
        List<Provider> list = new ArrayList<>();
        try {
            conn = BaseDao.getConnection();
            list = providerDao.queryProvider(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return list;
    }

    // 增加供应商
    @Override
    public boolean add(Provider provider) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            int updateRows = providerDao.add(conn, provider);
            conn.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            try {
                System.out.println("rollback()============================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    // 通过供应商名称、编码获取供应商列表-模糊查询-providerList
    @Override
    public List<Provider> getProviderList(String proName, String proCode) {
        Connection conn = null;
        List<Provider> list = new ArrayList<>();
        try {
            conn = BaseDao.getConnection();
            list = providerDao.getProviderList(conn, proName, proCode);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return list;
    }

    // 通过proId删除Provider
    @Override
    public boolean deleteProviderById(int delId) {
        Connection conn = null;
        boolean flag = false;
        int updateRows = 0;
        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            updateRows = providerDao.deleteProviderById(conn, delId);
            conn.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            try {
                System.out.println("rollback()============================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    // 通过proId获取Provider
    @Override
    public Provider getProviderById(int id) {
        Connection conn = null;
        Provider provider = new Provider();
        try {
            conn = BaseDao.getConnection();
            provider = providerDao.getProviderById(conn, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return provider;
    }

    // 修改供应商信息
    @Override
    public boolean modify(Provider provider) {
        Connection conn = null;
        boolean flag = false;

        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            int updateRows = providerDao.modify(conn, provider);
            conn.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            try {
                System.out.println("rollback()============================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
        return flag;
    }

    // 根据proCode查询供应商是否存在，用于检查供应商编码唯一性
    @Override
    public Provider selectProCodeExist(String proCode) {
        Connection conn = null;
        Provider provider = null;
        try {
            conn = BaseDao.getConnection();
            provider = providerDao.selectProCodeExist(conn, proCode);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return provider;
    }

}
